define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'manufacture/bom/index' + location.search,
                    add_url: 'manufacture/bom/add',
                    edit_url: 'manufacture/bom/edit',
                    del_url: 'manufacture/bom/del',
                    multi_url: 'manufacture/bom/multi',
                    table: 'bom',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'bom_no', title: __('BOM编号'), operate: 'LIKE'},
                        {field: 'bom_name', title: __('BOM名称'), operate: 'LIKE'},
                        {field: 'product_model.model_name', title: __('产品型号'), operate: false},
                        {field: 'version', title: __('版本'), align: 'center'},
                        {field: 'status', title: __('状态'), searchList: {"1":__('启用'),"0":__('禁用')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, formatter: function(value, row, index) {
                            var table = this.table;
                            var buttons = [];
                            buttons.push({
                                name: 'detail',
                                text: __('详情'),
                                title: __('查看BOM详情'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-eye',
                                url: 'manufacture/bom/detail/ids/{ids}',
                                callback: function (data) {
                                    table.bootstrapTable('refresh');
                                }
                            });
                            return Table.api.formatter.operate.call(this, value, row, index, buttons);
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                
                // 动态添加物料行
                $('#add-material-row').click(function() {
                    var newRow = $('.material-row:first').clone();
                    newRow.find('select').val('');
                    newRow.find('input').val('1');
                    $('#material-table tbody').append(newRow);
                });
                
                // 删除物料行
                $(document).on('click', '.remove-row', function() {
                    if ($('.material-row').length > 1) {
                        $(this).closest('.material-row').remove();
                    } else {
                        Toastr.error('至少需要保留一个物料');
                    }
                });
            }
        }
    };
    return Controller;
});
